package com.sanno.user.config;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sanno.common.entity.SncUserLogin;
import com.sanno.user.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

//@Component
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        String username = token.getUsername();
        QueryWrapper<SncUserLogin> queryWrapper = new QueryWrapper<SncUserLogin>();
        queryWrapper.eq("name",username);
        SncUserLogin sncUserLogin = userService.getOne(queryWrapper);
        System.out.println(username+"用户名");
        System.out.println(sncUserLogin);
        if(sncUserLogin==null){
            return null;
        }
        return new SimpleAuthenticationInfo(sncUserLogin.getName(),sncUserLogin.getPassword(),this.getName());
   }

}
